package com.litchishell;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LitchiShell
 * @description:
 * @create 2022-02-02
 */
public class GameWin extends JFrame {

    /* 游戏状态：1 运行中、2 商店、3 失败、4 胜利 */
    static int GAME_STATE;

    /* 黄金数量 */
    static int GOLD_NUM = 6;

    /* 石块数量 */
    static int ROCK_NUM = 3;

    /* 游戏背景 */
    Background bg = new Background();

    /* 红线 */
    Line line = new Line(this);

    /* 双缓存解决闪烁问题 */
    Image offScreenImage;

    /* 存放金块石块 */
    List<CommonObject> objects = new ArrayList<>();

    {
        boolean isPlace = true;
        for (int i = 0; i < GOLD_NUM; i++) {
            double random = Math.random();
            Gold gold;
            if (random < 0.3) {
                gold = new minGold();
            } else if (random < 0.7) {
                gold = new Gold();
            } else {
                gold = new maxGold();
            }

            for (CommonObject obj : objects) {
                if (gold.getRect().intersects(obj.getRect())) {
                    isPlace = false;
                    i--;
                }
            }

            if (isPlace) {
                objects.add(gold);
            } else {
                isPlace = true;
                i--;
            }
        }

        for (int i = 0; i < ROCK_NUM; i++) {
            Rock rock = new Rock();
            for (CommonObject object : objects) {
                if (rock.getRect().intersects(object.getRect())) {
                    isPlace = false;
                }
            }
            if (isPlace) {
                objects.add(rock);
            } else {
                isPlace = true;
                i--;
            }
        }
    }

    void launch() {
        /* 窗口是否可见 */
        this.setVisible(true);
        /* 窗口大小 */
        this.setSize(768, 1000);
        /* 窗口位置 */
        this.setLocationRelativeTo(null);
        /* 窗口标题 */
        this.setTitle("黄金矿工小游戏");
        /* 窗口关闭方式 */
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                switch (GAME_STATE) {
                    case 0:
                        if (e.getButton() == 3) {
                            GAME_STATE = 1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        /* 左右摇摆，点击左键可进行抓取 */
                        if (e.getButton() == 1 && line.state == 0) {
                            line.state = 1;
                        }

                        /* 抓取返回,点击右键*/
                        if (e.getButton() == 3 && line.state == 3) {
                            Background.OPTION_FLAG = true;
                            Background.POTION_NUM--;
                        }
                        break;
                    case 2:
                        if (e.getButton() == 1) {
                            bg.shop = true;
                        }
                        if (e.getButton() == 3) {
                            GAME_STATE = 1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        if (e.getButton() == 1) {
                            GAME_STATE = 0;
                            bg.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }


            }
        });

        while(true) {
            repaint();

            nextLevel();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void nextLevel(){
        if (GAME_STATE == 1 && bg.gameTime()) {
            if (Background.SUM > bg.goal) {

                if (Background.LEVEL == 5) {
                    GAME_STATE = 4;
                } else {
                    GAME_STATE = 2;
                    Background.LEVEL++;
                }
            } else {
                GAME_STATE = 3;
            }

            dispose();
            GameWin gameWin = new GameWin();
            gameWin.launch();
        }
    }

    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768, 1000);
        Graphics graphics = offScreenImage.getGraphics();

        bg.printSelf(graphics);

        if (GAME_STATE == 1) {
            for (CommonObject object : objects) {
                object.paintSelf(graphics);
            }

            line.paintSelf(graphics);
        }

        g.drawImage(offScreenImage, 0, 0, null);
    }

    /* 程序入口 */
    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
